﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using DevelopTool.Bll.ManageMenuBlls;
using DevelopTool.Bll.RemoteBlls;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.CommonControls;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using DevelopTool.Model.RemoteModels;
using Sunny.UI;

namespace DevelopTool.Forms.RemoteForms
{
    /// <summary>
    /// 远程管理
    /// </summary>
    public partial class RemoteManageForm : BaseUIPage
    {
        private ManageMenuBll manageMenull;

        private RemoteBll remoteBll;

        public RemoteManageForm()
        {
            InitializeComponent();
        }

        private void RemoteManageForm_Load(object sender,EventArgs e)
        {
            manageMenull = new ManageMenuBll();
            remoteBll = new RemoteBll();

            this.manageControl.BaseBll = remoteBll;

            LoadManageControl();
        }

        /// <summary>
        /// 加载管理页控件
        /// </summary>
        private void LoadManageControl()
        {
            List<ManageMenu> manageMenus = new List<ManageMenu>();
            LoadingHelper.ShowLoading("加载菜单数据",() =>
            {
                manageMenus = remoteBll.GetList().Cast<ManageMenu>().ToList();
            });

            this.manageControl.SetMenuData(manageMenus);
        }


        /// <summary>
        /// 菜单项添加时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuAdded(object sender,MenuEventArgs e)
        {
            UINavMenu nav = sender as UINavMenu;

            Remote remote = remoteBll.GetById(e.ManageMenu.Id);

            if(e.TreeNode.Name == "TopNode")
            {
                nav.SetNodeSymbol(e.TreeNode,61641);
            }
            else if(e.TreeNode.Name == "Folder")
            {
                nav.SetNodeSymbol(e.TreeNode,61564);
            }
            else if(e.TreeNode.Name == "Node")
            {
                nav.SetNodeSymbol(e.TreeNode,GetSymbol(remote.RemoteType));
            }
        }

        /// <summary>
        /// 根据远程类型获取图标
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private int GetSymbol(string type)
        {
            if(type == "RDP")
            {
                return 161818;
            }
            else if(type == "SSH")
            {
                return 161820;
            }
            else
            {
                return 361788;
            }
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="text"></param>
        private void ManageControl_MenuQuery(object sender,string text)
        {
            Dictionary<string,List<ManageMenu>> dic = new Dictionary<string,List<ManageMenu>>();

            //关联到的节点名
            var nameList = manageMenull.GetIdListByName<Remote>(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的任务名",nameList);

            var timeList = manageMenull.GetIdListByTime<Remote>(text).Cast<ManageMenu>().ToList();
            dic.Add("关联到的日期",timeList);

            this.manageControl.SetMenuQueryData(dic);
        }


        /// <summary>
        /// 菜单节点双击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDoubleClick(object sender,MenuEventArgs e)
        {
            if(e.ManageMenu.NodeType == MenuNodeType.Node)
            {
                Remote remote = remoteBll.GetById(e.ManageMenu.Id);

                if(remote.RemoteType == "RDP")
                {
                    this.manageControl.ShowPageForm(new RdpForm(remote),remote.NodeName,true,remote.NodeName + remote.Id);
                }
            }
        }


        /// <summary>
        /// 菜单节点拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDrag(object sender,MenuEventArgs e)
        {
            Remote remote = manageMenull.GetExample<Remote>(e.ManageMenu);

            manageMenull.SimpleSave(remote);
        }


        /// <summary>
        /// 删除菜单节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuDelete(object sender,MenuEventArgs e)
        {
            Remote remote = manageMenull.GetExample<Remote>(e.ManageMenu);

            if(remoteBll.Delete(remote.Id))
            {
                this.ShowSuccessNotifier($"【{remote.NodeName}】已删除");
            }
            else
            {
                e.Cancel = true;
                this.ShowWarningNotifier("不能删除非空目录");
            }
        }


        /// <summary>
        /// 重新加载菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuNeedReLoad(object sender,EventArgs e)
        {
            LoadManageControl();
        }




        /// <summary>
        /// 菜单节点重命名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuRename(object sender,MenuEventArgs e)
        {
            Remote remote = manageMenull.GetExample<Remote>(e.ManageMenu);

            manageMenull.SimpleSave(remote);
        }


        /// <summary>
        /// 新建菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageControl_MenuAdd(object sender,MenuEventArgs e)
        {
            Remote newRemote = null;

            //自定义创建节点
            if(e.ManageMenu.NodeType == MenuNodeType.Node)
            {
                List<string> items = new List<string>() { "RDP" };
                int index = 0;
                if(this.ShowSelectDialog(ref index,items,"请选择远程类型",string.Empty))
                {
                    e.ManageMenu.NodeName = e.ManageMenu.NodeName.Replace("节点",items[index]);

                    newRemote = manageMenull.GetExample<Remote>(e.ManageMenu);
                    newRemote.RemoteType = items[index];
                }
            }
            else
            {
                newRemote = manageMenull.GetExample<Remote>(e.ManageMenu);
            }

            newRemote = manageMenull.SimpleSave(newRemote);

            e.ManageMenu = newRemote;

            this.ShowSuccessNotifier("新建成功");

        }
    }
}
